import Ember from 'ember';
const { callStatus_noCall,callStatus_calling, callStatus_inCall,callStatus_callEnd,callStatus_callFail } = Constants;

/*线程支持*/
export default Ember.Service.extend({
  //状态监控线程
  statusWorker:null,
  global_curStatus:Ember.inject.service(),
  store: Ember.inject.service('store'),

  init() {
    var _self = this;
    this._super(...arguments);
    //创建状态监控的worker
    this.statusWorker = this.createWorker("statusWorker",function(result,callback){
      //定时请求当前通话状态以及任务状态
      _self.get("store").findAll("busistatus",{ include: 'curCall' }).then(function(busistatus){
        busistatus.forEach(function(status){
          //console.log("status inner:" ,status);
          status.get("curCall").then(function(curCall){
            var flag = null;
            if(curCall){
              flag = curCall.get("status");
            }else{
              flag = callStatus_noCall;
            }
            //根据号码取得客户
            _self.get("global_curStatus").findCustomerWithCall(curCall.get("callNumber")).then(function(customerEnt){
              //console.log("current call status:" + curCall.get("status"));
              curCall.set("customer",customerEnt);
              //_self.get("global_curStatus").setTask(curtask);
              //先放置通话信息，再置标志，后续页面会通过observe进行触发
              _self.get("global_curStatus").setCall(curCall);
              _self.get("global_curStatus").set("flag",flag);
              callback();
            });
          });
        });
      });
    });
    //启动状态监控
    Ember.run.later(function(){
      //_self.statusWorker.start();
    },1000);
  },
  //创建worker
  createWorker: function(workerName,mainProcess){
    var _self = this;
    var Worker = Ember.Object.extend(Ember.Evented, {
      workerName: workerName,
      callDuration:3,//轮询间隔，单位秒
      operative:null,
      isRunning: false,//运行标志
      init: function() {
        this.operative = operative({
          process: function(callback){
            callback();
          }
        });
      },
      start: function() {
        this.isRunning = true;
        this.trigger('start');
        this.run();
      },
      run: function(){
        var that = this;
        this.operative.process(function(result){
          Ember.run.later(that, function() {
            mainProcess(result,function(){
              //如果未停止，间隔后继续发请求
              if(!_self.isRunning){
                //console.log("need again");
                that.run();
              }else{
                //console.log("end run");
              }
            });
          }, that.callDuration*1000);
        });
      },
      stop: function() {
        this.isRunning = false;
        this.trigger('stop');
      }
    });
    var worker = Worker.create();
    worker.one("stop",function(){
      console.log("stop event in");
    });
    return worker;
  },
  /*开启状态监控线程*/
  startStatusWorker: function(){
    var curStatus = this.get("curStatus");
    curStatus.currentUser = user;
    this.set("curStatus",curStatus);
  },
});
